package cn.cvs.controller;

import cn.cvs.pojo.*;
import cn.cvs.service.forum.ForumService;
import cn.cvs.service.forumcomment.ForumCommentService;
import cn.cvs.utils.Constants;
import cn.cvs.utils.PageSupport;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpSession;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

@Controller
@RequestMapping("/sys/forum")
public class ForumController  extends BaseController {
    @Autowired
    private ForumService forumService;

    @Autowired
    private ForumCommentService forumCommentService;




    @GetMapping("/detail")
    public String getUserById(@RequestParam Integer id,Model model)throws Exception {

        Forum forum = forumService.getForumById(id);
        model.addAttribute(forum);

        List<ForumComment> list = forumCommentService.getForumCommentById(id);
        model.addAttribute("list", list);
        model.addAttribute("forum", forum);
        model.addAttribute("id", id);


        return "forum/detail";
    }

    @RequestMapping(value="/add")
    @ResponseBody
    public String add(HttpSession session,Forum forum){
        SysUser sysUser = (SysUser) session.getAttribute(Constants.USER_SESSION);
        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");
        forum.setManagerId(1);
        forum.setUserId(sysUser.getId());
        forum.setTime( format.format(new Date()));
        int count =forumService.addForum(forum);
        if(count>0){
            return "true";
        }else{
            return  "false";
        }
    }

    @RequestMapping(value="/list")
    public String getUserList(Model model, @RequestParam(value="pageIndex",required=false, defaultValue = "1") Integer pageIndex){
        List<Forum> userList = null;
        //设置页面容量
        int pageSize = Constants.pageSize;
        try{
            //总数量（表）
            int totalCount	= forumService.getCount();
            //总页数
            PageSupport pages=new PageSupport();
            pages.setCurrentPageNo(pageIndex);
            pages.setPageSize(pageSize);
            pages.setTotalCount(totalCount);
            int totalPageCount = pages.getTotalPageCount();
            //控制首页和尾页
            if(pageIndex > totalPageCount){
                pageIndex = totalPageCount;
            }
            userList = forumService.list(pageIndex,pageSize);
            model.addAttribute("list", userList);
            List<SysRole> roleList = null;


            model.addAttribute("totalPageCount", totalPageCount);
            model.addAttribute("totalCount", totalCount);
            model.addAttribute("currentPageNo", pageIndex);
        }catch(Exception e){
            e.printStackTrace();
            return "redirect:/sys/toSysError";
        }
        return "forum/list";
    }

    @RequestMapping(value="/toAdd")
    public void toAdd(){

    }


    @RequestMapping(value="/toList")
    public String getUserList(Model model
            ,@RequestParam(value="state",required=false) Integer state
            ,@RequestParam(value="pageIndex",required=false, defaultValue = "1") Integer pageIndex){

        //设置页面容量
        int pageSize = Constants.pageSize;
        try{
            //总数量（表）
            int totalCount	= forumService.count(state);
            //总页数
            PageSupport pages=new PageSupport();
            pages.setCurrentPageNo(pageIndex);
            pages.setPageSize(pageSize);
            pages.setTotalCount(totalCount);
            int totalPageCount = pages.getTotalPageCount();
            //控制首页和尾页
            if(pageIndex > totalPageCount){
                pageIndex = totalPageCount;
            }
            List<Forum> forums = forumService.listPage(state, pageIndex, pageSize);
            model.addAttribute("forums", forums);
            model.addAttribute("totalPageCount", totalPageCount);
            model.addAttribute("totalCount", totalCount);
            model.addAttribute("currentPageNo", pageIndex);
        }catch(Exception e){
            e.printStackTrace();
            return "redirect:/sys/toSysError";
        }
        return "forum/toList";
    }

    @GetMapping("/view/{id}")
    public String view(@PathVariable Integer id, Model model) {
        Forum forums = forumService.selectById(id);
        model.addAttribute("forums", forums);
        return "forum/update"; // 返回的视图名，对应上面的HTML文件名
    }

    @PostMapping("/update")
    public String view(Forum forum) {
        int count = forumService.updateById(forum);
        if (count > 0 ) {
            return "redirect:/sys/forum/toList";
        }
        return "forum/update"; // 返回的视图名，对应上面的HTML文件名
    }
}
